package com.liuyxbiu.cloud.controller;

import com.liuyxbiu.cloud.entities.Pay;
import com.liuyxbiu.cloud.entities.PayDTO;
import com.liuyxbiu.cloud.resp.ResultData;
import com.liuyxbiu.cloud.resp.ReturnCodeEnum;
import com.liuyxbiu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        log.info(pay.toString());
        int add = payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+add);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){
        int delete = payService.delete(id);
        return ResultData.success(delete);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        // 拷贝属性
        BeanUtils.copyProperties(payDTO,pay);
        int update = payService.update(pay);
        return ResultData.success("成功修改记录，返回值："+update);
    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        if (id<0){
            throw new RuntimeException("id值不能小于0");
        }
        return ResultData.success(payService.getById(id));
    }

    @GetMapping(value = "/pay/getAll")
    @Operation(summary = "查询所有流水",description = "查询支付流水方法")
    public ResultData<List<Pay>> getById(){
        return ResultData.success(payService.getAll());
    }

    // 不使用全局异常处理类，自己手动抛出
    @GetMapping(value = "/pay/error")
    public ResultData<Integer> getPageError(){
        Integer i = Integer.valueOf(100);
        try {
            log.info("come in page error test");
            int a=1/0;
        }catch (Exception e){
            log.error("正在处理error");
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), "已经处理异常了");
        }
        return ResultData.success(i);
    }
}